<?php session_start(); require_once('connections/con_gl.php'); ?>

<?php
$today = date("Y/m/d");
$month = date("Y/m");
cekAkses($_SESSION[akses],"IK-1-1");
// ---- get kontrak
mysql_select_db($database_con_gl, $con_gl);

$query_kontrak = "select no from gl_kontrak where sampai >='$today' and area ='$_SESSION[area]';";
/*
if(!strstr($_SESSION[akses],'A-1-4')) {
	$query_kontrak .= " AND gudang='$_SESSION[lokasi]'";
}
*/
//$query_kontrak .=" order by mulai";
//print_r($query_kontrak);
$kontrak = mysql_query($query_kontrak, $con_gl) or die(mysql_error());
$row_kontrak = mysql_fetch_assoc($kontrak);
$totalRows_kontrak = mysql_num_rows($kontrak);
//----
mysql_select_db($database_con_gl, $con_gl);

$query_kontak = "SELECT id, nama, type, tcustomer FROM gl_kontak where gl_kontak.type='Customer' AND status='AKTIF' ORDER BY type, nama ASC";

$kontak = mysql_query($query_kontak, $con_gl) or die(mysql_error());

$row_kontak = mysql_fetch_assoc($kontak);

$totalRows_kontak = mysql_num_rows($kontak);

// ---

mysql_select_db($database_con_gl, $con_gl);

$query_gudang = "SELECT id, gudang FROM gl_gudang";

if(!strstr($_SESSION[akses],'A-1-4')) {

	$query_gudang .= " where id='$_SESSION[lokasi]'";

}

$query_gudang .=" ORDER BY gudang ASC";

$gudang = mysql_query($query_gudang, $con_gl) or die(mysql_error());

$row_gudang = mysql_fetch_assoc($gudang);

$totalRows_gudang = mysql_num_rows($gudang);



mysql_select_db($database_con_gl, $con_gl);

$query_satuan = "SELECT id, satuan FROM gl_satuan ORDER BY satuan ASC";

$satuan = mysql_query($query_satuan, $con_gl) or die(mysql_error());

$row_satuan = mysql_fetch_assoc($satuan);

$totalRows_satuan = mysql_num_rows($satuan);



mysql_select_db($database_con_gl, $con_gl);

$query_kelom = "SELECT id, kelompok FROM gl_kelompok where jual=1 ORDER BY kelompok ASC";

$kelom = mysql_query($query_kelom, $con_gl) or die(mysql_error());

$row_kelom = mysql_fetch_assoc($kelom);

$totalRows_kelom = mysql_num_rows($kelom);



mysql_select_db($database_con_gl, $con_gl);

$query_klas = "SELECT kd, klasifikasi, keterangan, neraca, ruglab, tipe FROM gl_klas where tipe = 6 ORDER BY kd,klasifikasi ASC";

$klas = mysql_query($query_klas, $con_gl) or die(mysql_error());

$row_klas = mysql_fetch_assoc($klas);

$totalRows_klas = mysql_num_rows($klas);



mysql_select_db($database_con_gl, $con_gl);

$query_def_ak = "SELECT gl_company.a_piu FROM gl_company";

$def_ak = mysql_query($query_def_ak, $con_gl) or die(mysql_error());

$row_def_ak = mysql_fetch_assoc($def_ak);

$totalRows_def_ak = mysql_num_rows($def_ak);



$totdeb = 0;

$totkre = 0;

?>

<link rel="stylesheet" type="text/css" href="css/ui.datepicker.css"/>

<script type="text/javascript" src="js/ajax_data.js"></script>

<script type="text/javascript" src="js/ui.datepicker.js"></script>

<script type="text/javascript">

<!--

$(function()

      {

        $('.calendar').datepicker({

            appendText : "yyyy/mm/dd",

            dateFormat : 'yy/mm/dd'

          });

      });

</script>

<script type="text/javascript">

$(document).ready(function() {



	$().ajaxStart(function() {

		$('#loading').show();

		$('#result').hide();

	}).ajaxStop(function() {

		$('#loading').hide();

		$('#result').fadeIn('slow');

	});



	$('#add').submit(function() {

		$.ajax({

			type: 'POST',

			url: $(this).attr('action'),

			data: $(this).serialize(),

			success: function(data) {

				$('#result').html(data);

			}

		})

		return false;

	});

  $('#result').click(function(){

  $(this).hide();

  });

})

</script>

<div id="loading" style="display:none;"><img src="images/loading.gif" alt="loading..." /></div>

<div id="result" style="display:none;"></div>

<h1>Generated Invoice Kontrak</h1>

<form action="proses/kinvoice.php?act=add" method="post" name="add" id="add">

  <table width="98%" border="0" align="center" cellpadding="5" cellspacing="0" class="datatable">

    <tr valign="top">

      <td width="12%" align="right">No. Kontrak :</td>

      <td width="32%"><label>
        <select name="kontrak" id="kontrak" style="width:230px;" onchange="genTglKontrak(this.value);genAreaKontrak(this.value);genCabangKontrak(this.value);genCustomerKontrak(this.value);genKeteranganKontrak(this.value);genSKendaraanKontrak(this.value);">
          <option value="">Pilih No. Kontrak</option>
          <?php if($totalRows_kontrak > 0) { do { ?>
          <?php
		  // check invoice already exist
		  $query_cinvoice = "select no_ref from gl_retailtrans where kontrak='$row_kontrak[no]' AND tgl LIKE '%$month%'";
		  $cinvoice = mysql_query($query_cinvoice);
		  $total_cinvoice = mysql_num_rows($cinvoice);
		  // cek periode
		  $query_pinvoice = "select mulai, sampai, durasi, pbayar from gl_kontrak where no='$row_kontrak[no]'";
		  $pinvoice = mysql_query($query_pinvoice);
		  $row_pinvoice = mysql_fetch_assoc($pinvoice);
		  // ---
		  $jloop = $row_pinvoice['durasi'] / $row_pinvoice['pbayar'];
		  for($ix=1;$ix<$jloop;$ix++) {
			$nix = $ix - 1;
			$per_ = date("Y/m", strtotime("$row_pinvoice[mulai] +$nix month-1 day"));
			if($per_ == $month) {
				$valid = 1;
				break;
			}
		  }
		  if(!$total_cinvoice AND $valid=='1') { 
		  ?>
          <option value="<?php echo $row_kontrak['no'];?>"><?php echo $row_kontrak['no'];?></option>
          <?php } }while($row_kontrak = mysql_fetch_assoc($kontrak)); } ?>
        </select>
      </label></td>

      <td width="15%" align="right">&nbsp;</td>

      <td width="41%"><label></label></td>

    </tr>

    <tr>

      <td align="right" valign="top">No. Referensi :</td>

      <td valign="top"><div id="refDiv">
        <label>
          <input name="noreferensi" type="text" id="noreferensi" value="SJ<?php echo time();?>" size="30" maxlength="20" onchange="cekReferensi(this.value);"/>
        </label>
      </div></td>

      <td align="right" valign="top">Area &amp; Cabang  :</td>

      <td valign="top"><label id="divArea">
          <select name="area" id="area" style="width:150px;visibility:hidden;" >
            <option value="">Area Operasional</option>
          </select>
      </label>
	  <label id="divCabang"><select name="gudang" id="gudang" style="width:160px;visibility:hidden;">

        <?php

do {  

?>

        <option value="<?php echo $row_gudang['id']?>"><?php echo $row_gudang['gudang']?></option>

        <?php

} while ($row_gudang = mysql_fetch_assoc($gudang));

  $rows = mysql_num_rows($gudang);

  if($rows > 0) {

      mysql_data_seek($gudang, 0);

	  $row_gudang = mysql_fetch_assoc($gudang);

  }

?>

      </select></label></td>

    </tr>

    <tr>

      <td align="right" valign="top">Periode :</td>
      <td valign="top"><label id="divTanggal">
        <input name="tanggal" type="text" id="tanggal" size="12" maxlength="12" readonly="readonly"/> s.d <input name="jatuhtempo" type="text" id="jatuhtempo" size="12" maxlength="12" readonly="readonly"/>
      </label></td>

      <td align="right" valign="top">Customer :</td>
      <td valign="top"><label></label>
        <label id="divKontak">
          <select name="dari" id="dari" style="width:310px;visibility:hidden;" onchange="setKeterangan(this.value);">
            <option value="">Pilih Customer</option>
            <?php

do {  

?>
            <option value="<?php echo $row_kontak['id']?>"><?php echo $row_kontak['nama']?></option>
            <?php

} while ($row_kontak = mysql_fetch_assoc($kontak));

  $rows = mysql_num_rows($kontak);

  if($rows > 0) {

      mysql_data_seek($kontak, 0);

	  $row_kontak = mysql_fetch_assoc($kontak);

  }

?>
          </select>
        </label></td>

    </tr>
    <tr>

      <td align="right" valign="top">&nbsp;</td>
      <td valign="top">&nbsp;</td>

      <td align="right" valign="top">Keterangan :</td>

      <td valign="top"><div id="divKeterangan">

        <textarea name="keterangan" cols="40" rows="2" id="keterangan" readonly="readonly"></textarea>

        <input name="cid" type="hidden" id="cid" value=""/>

      </div></td>

    </tr>

    <tr>

      <td colspan="4" align="right"><div id="ajaxDiv">

        <table width="100%" border="0" cellspacing="0" cellpadding="5" class="datatable">

          <tr>

            <th width="42%" align="center"><strong>Daftar Kendaraan</strong></th>

            <th width="5%" align="center"><strong>Satuan</strong></th>

            <th width="5%" align="center"><strong>Jumlah</strong></th>

            <th width="8%" align="center"><strong>Harga Satuan </strong></th>

            <th width="5%" align="center"><strong>Diskon</strong></th>

            <th width="6%" align="center"><strong>Total</strong></th>

            <th align="center"><strong>Transaksi</strong></th>

            </tr>

          <tr valign="top" bgcolor="#FFFFFF">

            <td align="left" style="background:none;border:none;"><label id="divListKendaraan">
              
              <select name="kendaraan[]" id="kendaraan[]" style="width:100%;">

                  <option value="">Pilih Kendaraan</option>
				
                </select>

              </label></td>

            <td align="center" style="background:none;border:none;"><label>

              <select name="satuan[]" id="satuan[]">

                <?php

do {  

?>

                <option value="<?php echo $row_satuan['id']?>"><?php echo $row_satuan['satuan']?></option>

                <?php

} while ($row_satuan = mysql_fetch_assoc($satuan));

  $rows = mysql_num_rows($satuan);

  if($rows > 0) {

      mysql_data_seek($satuan, 0);

	  $row_satuan = mysql_fetch_assoc($satuan);

  }

?>

              </select>

            </label></td>

            <td align="center" style="background:none;border:none;"><label>

              <input name="jumlah[]" type="text" id="jumlah[]" onchange="javascript:this.form.total.value=(this.form.jumlah.value * this.form.hargasatuan.value) - (this.form.jumlah.value * this.form.hargasatuan.value * this.form.diskon.value / 100);" size="5" maxlength="5" readonly="readonly"/>

            </label></td>

            <td align="center" style="background:none;border:none;"><label id="divHargaKendaraan">

              <input name="hargasatuan[]" type="text" id="hargasatuan[]" onchange="javascript:this.form.total.value=(this.form.jumlah.value * this.form.hargasatuan.value) - (this.form.jumlah.value * this.form.hargasatuan.value * this.form.diskon.value / 100);" size="10" maxlength="10" readonly="readonly"/>

            </label></td>

            <td align="center" style="background:none;border:none;"><label>

              <input name="diskon[]" type="text" id="diskon[]" onchange="javascript:this.form.total.value=(this.form.jumlah.value * this.form.hargasatuan.value) - (this.form.jumlah.value * this.form.hargasatuan.value * this.form.diskon.value / 100);" value="0" size="2" maxlength="3" readonly="readonly"/>

              </label>

              %</td>

            <td align="center" style="background:none;border:none;"><label>

              <input name="total[]" type="text" id="total[]" size="10" maxlength="15" readonly="readonly"/>

            </label></td>

            <td align="center" style="background:none;border:none;"><label>
              
              <textarea name="transaksi[]" cols="30" rows="2" id="transaksi[]" readonly="readonly"></textarea>
              
            </label></td>

            </tr>

        </table>

      </div></td>

    </tr>
    <tr>

      <td align="right">&nbsp;</td>

      <td colspan="3" align="right"><input name="page" type="hidden" id="page" value="jual" />

      <input name="jenis" type="hidden" id="jenis" value="7" />

          <label>

          <input name="Save" type="submit" id="Save" value="Simpan Penjualan" />

        </label></td>

    </tr>

  </table>

  <input type="hidden" name="MM_update" value="add" />

  <input type="hidden" name="MM_insert" value="add" />

</form>

